<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户注册</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery-validate/1.19.2/jquery.validate.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery-validate/1.19.2/additional-methods.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <h2>用户信息注册</h2>
    </div>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="#" id="regform" class="form-horizontal">
                <div class="form-group">
                    <label for="username" class="control-label col-md-2">用户名</label>
                    <div class="col-md-10">
                        <input type="text" class="form-control" id="username" name="username"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="control-label col-md-2">密码</label>
                    <div class="col-md-10">
                        <input type="password" class="form-control" id="password" name="password"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="confirm" class="control-label col-md-2">重复密码</label>
                    <div class="col-md-10">
                        <input type="password" class="form-control" id="confirm" name="confirm"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="control-label col-md-2">邮箱</label>
                    <div class="col-md-10">
                        <input type="email" class="form-control" id="email" name="email"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="website" class="control-label col-md-2">网址</label>
                    <div class="col-md-10">
                        <input type="url" class="form-control" id="website" name="website"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="agree" class="control-label col-md-2"></label>
                    <div class="col-md-10">
                        <label for="agree">同意协议</label>
                        <input type="checkbox" id="agree" name="agree"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-4">
                        <input type="submit" class="btn btn-primary" id="btnSubmit" value="提交">
                        <input type="reset" class="btn btn-default" value="重置">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>
<script>

    // 定义预验证规则和提示信息
    $().ready(function () {
        $('#regform').validate({
            rules: {
                username: {required: true, minlength: 2, maxlength: 20, remote: "{{url_for('login_blue.check_user')}}"},
                password: {required: true, minlength: 6},
                confirm: {required: true, equalTo: "#password"},
                email: {email: true},
                website: {url: true},
                agree: "required"
            },
            messages: {
                username: {required: '用户名必须输入', minlength: '用户名不少于2位', maxlength: '用户名不多于20位', remote: '该用户名已存在不可用'},
                password: {required: '密码不能空', minlength: '密码长度不少于6位'},
                confirm: {required: '重复密码不能空', equalTo: "两次密码不同"},
                email: {email: '邮箱格式不正确'},
                website: {url: '网址格式不正确'},
                agree: "必须同意协议才能继续！"
            },
            submitHandler: function (form) {
                $.ajax({
                    method: 'POST',
                    url: "{{url_for('login_blue.reg_user')}}",
                    data: $(form).serialize(),
                    success: function (resp) {
                        console.log(resp)
                    },
                });
            },
        });
    });
</script>